package com.sunjob.loginserver.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.config.SaSsoConfig;
import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.sso.SaSsoHandle;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.sunjob.common.constant.MROConstant;
import com.sunjob.common.domain.loginServer.SysRole;
import com.sunjob.common.domain.loginServer.SysUser;
import com.sunjob.common.domain.system.*;
import com.sunjob.common.mroAnno.ControllerLog;
import com.sunjob.common.mroAnno.LogModuleType;
import com.sunjob.common.result.Result;
import com.sunjob.common.util.MROUtils;
import com.sunjob.loginserver.service.UserService;
import com.sunjob.mrosystem.logrecorder.LogRecorder;
import com.sunjob.mrosystem.logrecorder.LogRecorderFactory;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;
import sun.util.resources.cldr.ml.CalendarData_ml_IN;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Wangzurong
 * @create 2022-07-24 18:28
 */
@Api(value = "登录与注销 权限与角色相关api", tags = "登录与注销 权限与角色相关api")
@RestController
@CrossOrigin
public class LoginServerController {
    @Autowired
    UserService userService;
    @Autowired
    RedisTemplate<String,Object> redisTemplate;
    /**
     * Sa-Token-SSO Server端 Controller
     * /sso/doLogin 登录接口
     */
    @ApiOperation(value = "登录")
    @PostMapping("/sso/doLogin")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "登录名",paramType = "query"),
            @ApiImplicitParam(name = "pwd",value = "密码",paramType = "query")
    })
    public Result<String> doLogin(@RequestParam("name") String name, @RequestParam("pwd") String pwd) {
        // name admin
        // pwd admin123
        return (Result<String>) SaSsoHandle.serverRequest();
    }

    @ApiOperation(value = "多端登录")
    @PostMapping("/sso/doLoginMulti")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "登录名",paramType = "query"),
            @ApiImplicitParam(name = "pwd",value = "密码",paramType = "query"),
            @ApiImplicitParam(name = "terminal",value = "终端",paramType = "query")
    })
    public Result<String> doLoginMulti(@RequestParam("name") String name,@RequestParam("pwd") String pwd,@RequestParam("terminal") String terminal){
        boolean res = userService.loginMulti(name,pwd,terminal);
        if(res){
            return Result.ok(StpUtil.getTokenValue(),"登录成功");
        }
        return Result.fail("登录失败，用户名或密码错误");
    }

    @ApiOperation(value = "注销")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "satoken",value = "token",paramType = "header")
    })
    @SaCheckLogin
    @GetMapping("/sso/logout")
    public Result<Object> doLogOut() {
        return Result.ok(SaSsoHandle.serverRequest(), "注销成功");
    }

    @ApiOperation(value = "批量注销")
    @SaCheckPermission("monitor:online:logout")
    @PostMapping("/sso/batchLogout")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids",value = "用户编号集合",paramType = "body"),
            @ApiImplicitParam(name = "satoken",value = "token",paramType = "header")
    })
    public Result<Integer> batchLogout(@RequestBody List<Integer> ids) {
        return Result.ok(userService.batchLogout(ids), "注销成功");
    }

    @ApiOperation("获取用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "satoken",value = "token",paramType = "header")
    })
    @SaCheckLogin
    @GetMapping("/getUserInfo")

    public Result<SysUser> getUserInfo(){
        SaSession tokenSession = StpUtil.getTokenSession();
        Object userInfo = tokenSession.get("userInfo");
        return Result.ok((SysUser)userInfo,"查询用户信息成功");
    }


    @SaCheckLogin
    @ApiOperation("获取用户角色信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "satoken",value = "token",paramType = "header")
    })
    @GetMapping("/getRole")
    public Result<List<String>> getRole() {
        return Result.ok(StpUtil.getRoleList(), "获取角色列表成功");
    }

    @ApiOperation("获取用户权限信息")
    @SaCheckLogin
    @ApiImplicitParams({
            @ApiImplicitParam(name = "satoken",value = "token",paramType = "header")
    })
    @GetMapping("/getAuth")
    public Result<List<String>> getAuth() {
        return Result.ok(StpUtil.getPermissionList(), "获取权限列表成功");
    }


}
